using Godot;
using System;
using System.Collections.Generic;

public class MainMenu : Control
{
    Vector2 center;
    int page = 0;
    Panel worldSetting, Worlds;
    Control view_0;
    Camera2D camera;
    Button Play, Exit, Create, Cancel;
    List<Button> buttons = new List<Button>();
    AnimationPlayer animation;
    public override void _Ready()
    {
        center = GetViewport().Size / 2;
        camera = GetNode<Camera2D>("Camera2D");
        worldSetting = GetNode<Panel>("Camera2D/WorldSetting");
        Worlds = GetNode<Panel>("Camera2D/Worlds");
        Worlds.Visible = false;
        worldSetting.Visible = false;
        view_0 = GetNode<Control>("Camera2D/view_0");
        animation = GetNode<AnimationPlayer>("AnimationPlayer");
        TextManage.Load();
        animation.Play("backgroundanim");
        for (int i = 0; i < 10; i++)
        {
            Button button = GetNode<Button>($"Camera2D/Worlds/{i}");
            button.Visible = false;
            Button b = button.GetNode<Button>("Close");
            b.SetTitle("删除");
            b.SetEvent((Button btn) =>
            {
                Button p = btn.GetParent<Button>();
                if (p.GetTitle() != "")
                {
                    Directory dir = new Directory();
                    if (dir.Open($"Save/{p.GetTitle()}") == Error.Ok)
                    {
                        OS.MoveToTrash($"Save/{p.GetTitle()}");
                        GD.Print(dir.Remove($"Save/{p.GetTitle()}/"));
                        GD.Print("remove at" + $"Save/{p.GetTitle()}/");
                        RestButton();
                    }
                }
            });
            buttons.Add(button);
        }

        Play = GetNode<Button>("Camera2D/view_0/Play").SetTitle("开始游戏").SetEvent((Button button) =>
        {
            Worlds.Visible = true;
            worldSetting.Visible = true;
            view_0.Visible = false;
        });
        Exit = GetNode<Button>("Camera2D/view_0/Exit").SetTitle("退出游戏").SetEvent((Button button) =>
        {
            GetTree().Quit(0);
        });
        Create = GetNode<Button>("Camera2D/WorldSetting/Create").SetTitle("创建世界").SetEvent((Button button) =>
        {
            BlockWorld.WorldName = GetNode<LineEdit>("Camera2D/WorldSetting/WorldName").Text;
            if (BlockWorld.WorldName == "") return;
            String text = GetNode<LineEdit>("Camera2D/WorldSetting/Seed").Text;
            if (text != "")
                BlockWorld.Seed = int.Parse(text);
            else BlockWorld.Seed = new Random().Next();
            GetTree().ChangeScene("res://Tscn/BlockWorld.tscn");
        });
        Cancel = GetNode<Button>("Camera2D/WorldSetting/Cancel").SetTitle("取消").SetEvent((Button button) =>
        {
            Worlds.Visible = false;
            worldSetting.Visible = false;
            view_0.Visible = true;
        });
        ModLoader.GetInstance().Run();
        ModLoader.GetInstance().CallAll("main");
        RestButton();

        //
        var air = Materials.Air;
    }

    public void RestButton()
    {
        List<String> worlds = FindWorlds();
        for (int i = 0; i < buttons.Count; i++)
        {
            Button button = buttons[i];
            if (i + page < worlds.Count)
            {
                button.Visible = true;
                button.SetTitle(worlds[i + page]);
                button.SetEvent((Button b) =>
                {
                    String title = b.GetTitle();
                    if (title != "")
                    {
                        BlockWorld.WorldName = title;
                        GetTree().ChangeScene("res://Tscn/BlockWorld.tscn");
                    }
                });
            }
            else
            {
                button.Visible = false;
            }
        }
    }

    public List<String> FindWorlds()
    {
        List<String> worlds = new List<string>();
        File file = new File();
        Directory dir = new Directory();
        if (dir.Open("Save") != Error.Ok)
        {
            dir.MakeDir("Save");
        }
        dir.ListDirBegin(true);
        String filename = dir.GetNext();
        while (filename != "")
        {
            if (dir.CurrentIsDir())
            {
                worlds.Add(filename);
            }
            filename = dir.GetNext();
        }
        dir.ListDirEnd();
        return worlds;
    }

    public override void _Process(float delta)
    {
        base._Process(delta);
    }

    public void ReSetWindowsSize()
    {
        center = (GetViewport().Size / 2);
    }
}